﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PublicFunction
{
    /// <summary>
    /// 自定义时间处理功能
    /// </summary>
    public class TimeCheck
    {
        /// <summary>
        /// 时间戳计时开始时间
        /// </summary>
        private static DateTime timeStampStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);



        /// <summary>
        /// 分钟间隔检测
        /// </summary>
        /// <param name="last"></param>
        /// <param name="nInterval"></param>
        /// <returns></returns>
        //public static bool MinuteTimeTick(ref int last, int nInterval = 5)
        //{
        //    int minute = DateTime.Now.Minute;

        //    if ((minute % nInterval) == 0 && minute != last)
        //    {
        //        last = minute;
        //        return true;
        //    }
        //    else
        //        return false;
        //}

        /// <summary>
        /// 毫
        /// </summary>
        /// <param name="interval"></param>
        /// <param name="lastTime"></param>
        /// <returns></returns>
        //public static bool MillisecondTimeTick(uint interval, ref DateTime lastTime)
        //{
        //    TimeSpan timeSpan = DateTime.Now.Subtract(lastTime);
        //    if (timeSpan.TotalMilliseconds >= interval)
        //    {
        //        lastTime = DateTime.Now;
        //        return true;
        //    }
        //    else
        //        return false;
        //}

        /// <summary>
        /// 秒间隔检测
        /// </summary>
        /// <param name="interval"></param>
        /// <param name="lastTime"></param>
        /// <returns></returns>
        public static bool SecondTimeTick(uint interval, ref DateTime lastTime)
        {
            TimeSpan timeSpan = DateTime.Now.Subtract(lastTime);
            if (timeSpan.TotalSeconds >= interval)
            {
                lastTime = DateTime.Now;
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// 分钟检测检测
        /// </summary>
        /// <param name="interval"></param>
        /// <param name="lastTime"></param>
        /// <returns></returns>
        public static bool MinuteTimeTick(uint interval, ref DateTime lastTime)
        {
            TimeSpan timeSpan = DateTime.Now.Subtract(lastTime);
            if (timeSpan.TotalMinutes >= interval)
            {
                lastTime = DateTime.Now;
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// DateTime转换为10位时间戳（单位：秒）
        /// </summary>
        /// <param name="dateTime"> DateTime</param>
        /// <returns>10位时间戳（单位：秒）</returns>
        //public static long DateTimeToTimeStamp(DateTime dateTime)
        //{
        //    return (long)(dateTime.ToUniversalTime() - timeStampStartTime).TotalSeconds;
        //}

        /// <summary>
        /// DateTime转换为13位时间戳（单位：毫秒）
        /// </summary>
        /// <param name="dateTime"> DateTime</param>
        /// <returns>13位时间戳（单位：毫秒）</returns>
        public static long DateTimeToLongTimeStamp(DateTime dateTime)
        {
            return (long)(dateTime.ToUniversalTime() - timeStampStartTime).TotalMilliseconds;
        }

        /// <summary>
        /// 10位时间戳（单位：秒）转换为DateTime
        /// </summary>
        /// <param name="timeStamp">10位时间戳（单位：秒）</param>
        /// <returns>DateTime</returns>
        //public static DateTime TimeStampToDateTime(long timeStamp)
        //{
        //    return timeStampStartTime.AddSeconds(timeStamp).ToLocalTime();
        //}

        /// <summary>
        /// 13位时间戳（单位：毫秒）转换为DateTime
        /// </summary>
        /// <param name="longTimeStamp">13位时间戳（单位：毫秒）</param>
        /// <returns>DateTime</returns>
        public static DateTime LongTimeStampToDateTime(long longTimeStamp)
        {
            return timeStampStartTime.AddMilliseconds(longTimeStamp).ToLocalTime();
        }
    }
}
